﻿using KAKA.Domain;
using KAKA.Repository;
using KAKA.Service;
using Morus;
using Morus.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KAKA.Infrastructure
{
    public class SendTextMessage : BaseBackgroundTask
    {
        private SubscriptionToken _verifyMobileToken;

        public SendTextMessage()
            : base(IoC.Resolve<IEventAggregator>())
        {
            TextMessageSender = IoC.Resolve<ITextMessageSender>();
        }

        public ITextMessageSender TextMessageSender { get; private set; }

        protected override void OnStart()
        {
            if (!IsRunning)
            {
                _verifyMobileToken = Subscribe<MobileVerificationEvent, MobileVerificationEventArgs>(VerifyMobile);
            }
        }

        private void VerifyMobile(MobileVerificationEventArgs eventArgs)
        {
            TextMessageSender.SendProof(eventArgs.Proof);
        }

        protected override void OnStop()
        {
            if (IsRunning)
            {
                Unsubscribe<MobileVerificationEvent>(_verifyMobileToken);
            }
        }
    }
}
